/*
 * Copyright 2021-2030 itbox.cn All right reserved. This software is the confidential and proprietary information
 * of itbox.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.cn.
 */
package cn.itbox.example.test.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.AES256TextEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongProject uap
 * @BelongPackage cn.itbox.example.test.controller
 * @Description TODO
 * @ClassName JasyptController
 * @Author xingc
 * @Date 2024/1/21 4:25
 * @Version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/jasypt")
public class JasyptController {
    @Value("${jasypt.encryptor.password}")
    private String salt;

    @Value("${redis.password}")
    private String redisPwd;

    @Value("${jdbc.root.password}")
    private String jdbcPwd;

    @Value("${jdbc.root.user}")
    private String jdbcUser;

    @Resource
    private StringEncryptor stringEncryptor;

    @GetMapping("/redis/pwd")
    public Map<String, String> redisPwd() {
        Map<String, String> map = new HashMap<>();
        map.put("redisPwd", redisPwd);
        map.put("jdbcPwd", jdbcPwd);
        map.put("jdbcUser", jdbcUser);
        return map;
    }

    @PostMapping("/encrypt")
    public String encrypt(@RequestBody String pwd) {
        String newPwd = stringEncryptor.encrypt(pwd);
        log.info("{} --> {}", pwd, newPwd);
        return newPwd;
    }

    @PostMapping("/decrypt")
    public String decrypt(@RequestBody String pwd) {
        String newPwd = stringEncryptor.decrypt(pwd);
        log.info("{} --> {}", pwd, newPwd);
        return newPwd;
    }

    @PostMapping("/encrypt256")
    public String encrypt256(@RequestBody String pwd) {
        AES256TextEncryptor encryptor = new AES256TextEncryptor();
        encryptor.setPassword(salt);
        String newPwd = encryptor.encrypt(pwd);
        log.info("{} --> {}", pwd, newPwd);
        return newPwd;
    }

    @PostMapping("/decrypt256")
    public String decrypt256(@RequestBody String pwd) {
        AES256TextEncryptor encryptor = new AES256TextEncryptor();
        encryptor.setPassword(salt);
        String newPwd = encryptor.decrypt(pwd);
        log.info("{} --> {}", pwd, newPwd);
        return newPwd;
    }
}
